package express;

import express.after.AfterExpressCalculate;
import express.before.BeforeExpressCalculate;
import express.check.ExpressCheck;
import express.check.Words;
import express.middle.MiddleExpressCalculate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 总测试流程
 *
 * @author miweidong
 * @date 2021-08-11 10:13
 */
public class Client {
    public static void main(String[] args) {
        String express = "2.1+1.1-2.3*(3+5)+6/2";
        ExpressCheck expressCheck = new ExpressCheck();
        Integer analysis = expressCheck.workAnalysis(express);
        if (analysis == -1) {
            System.out.println("表达式中存在非法字符");
            System.exit(0);
        }
        expressCheck.Next();
        expressCheck.E();
        boolean flag = expressCheck.getSym() == 0 && expressCheck.getErr() == 0;
        if (!flag) {
            System.out.println("表达式语法错误");
            System.exit(0);
        }
        List<Words> wordsList = expressCheck.getWords();
        List<String> words = wordsList.stream().map(Words::getLabel).collect(Collectors.toList());

        //拷贝字符序列，因为在计算时会给words添加终结符=，从而影响了后续计算
        List<String> middleSerialList = new ArrayList<>(words);
        List<String> beforeSerialList = new ArrayList<>(words);
        List<String> afterSerialList = new ArrayList<>(words);

        // 中缀计算
        IExpressCalculate middleExpress = new MiddleExpressCalculate();
        BigDecimal middle = middleExpress.calculate(middleSerialList);
        System.out.println("中缀表达式计算结果： " + middle);

        // 前缀计算
        IExpressCalculate beforeExpress = new BeforeExpressCalculate();
        BigDecimal before = beforeExpress.calculate(beforeSerialList);
        System.out.println("前缀表达式计算结果： " + before);

        // 后缀
        IExpressCalculate afterExpress = new AfterExpressCalculate();
        BigDecimal after = afterExpress.calculate(afterSerialList);
        System.out.println("后缀表达式计算结果： " + after);

    }
}
